package com.example.presenters;

import com.example.model.User;
import com.example.model.UserList;
import com.example.db.db;
import com.example.interfaces.IMainActivity;

/**
 * This is the main initiation controller. It will accept login info from the
 * MainActivity and validate it. It will also determine whether the user wants
 * to create a new account, and take the appropriate the action, i.e., initiate
 * the RegisterPresenter and Activity.
 * 
 * @author 3 Factorial.
 */
public class MainPresenter {

	// public final static String USERNAME = "com.example.first.USERNAME"
	private IMainActivity main;
	private UserList users;

	/**
	 * Initiates the Main presenter.
	 * 
	 * @param main
	 * @param users
	 */
	public MainPresenter(IMainActivity main, UserList users) {
		this.main = main;
		this.users = users;
		db dbaccess = new db();
		dbaccess.getUsers(this.users);
	}

	/**
	 * Establishes whether a user login information is valid.
	 * 
	 * @param sUsername
	 * @param sPassword
	 */
	public void login(String sUsername, String sPassword) {
		User u = users.getUserByUsername(sUsername);
		if (u != null)
			if (u.getPassword().equals(sPassword)) {
				if (!u.isLocked()) {
					main.goToProfile(sUsername);
				} else {
					main.dispLockedOut(sUsername);
				}
			} else {
				int i = u.hasBadPassword();
				if (i >= 3) {
					u.lock(true);
					main.dispLockedOut(sUsername);
				} else {
					main.dispWrongPassword();
				}
			}
		else {
			main.dispNoUserFound();
		}
	} // End login method

} // End MainPresenter class
